/*******************************************************************************
 * Copyright 2007 Vidar Svansson
 *     
 * This file is part of JAdapter.
 * 
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/


package org.jadapter.tests;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import junit.framework.TestCase;

import org.jadapter.Components;
import org.jadapter.JUtility;

public class TestUtility extends TestCase {

    @SuppressWarnings("unchecked")
    public void testGetUtility() throws Exception {

        Components.register(List.class, new LinkedList<Object>());

        List list = Components.get(List.class);
        assertNotNull("list is registered", list);

        Map map = Components.get(Map.class);
        assertNull("map is not registred", map);

        Collection collection = Components.get(Collection.class);
        assertNotNull("list implements collection", collection);

        list = Components.get(LinkedList.class);
        assertNotNull("LinkedList is implicitly available", list);

    }

    public void testRegisterInferType() throws Exception {
        Components.register(new TreeSet());

        Set set = Components.get(Set.class);
        assertNotNull("w", set);
    }

    @SuppressWarnings("unchecked")
    public void testJUtility(){
        JUtility util = new JUtility();
        util.setProvides(List.class);
        util.setUtility(new LinkedList());

        Components.register(util);

        List list = Components.get(List.class);
        assertNotNull("failed to use JUtility", list);

    }
    
    public void testContextBasedUtilities() throws Exception {
        
        Components.register(List.class, new LinkedList<String>(), String.class);
        Components.register(List.class, new ArrayList<Integer>(), Integer.class);
        
        List<?> list = Components.get(List.class, Integer.class);
        assertTrue("wrong context returned", list.getClass() == ArrayList.class);
        
        list = Components.get(List.class, String.class);
        assertTrue("wrong context returned", list.getClass() == LinkedList.class);        
        
    }    

}
